﻿from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class Libx264Conan(ConanFile):
    name = "libx264"
    version = "1.1.1"
    url = "https://gitee.com/290198252/libx264.git"
    description = "libx264"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    
    def source(self):
        git = tools.Git(folder="./", verify_ssl=False)
        git.clone(self.url,branch="master")

    def build(self):
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd=".", msys_mingw=True,
                                              bashcmd="./configure --enable-static --host=msys --disable-asm  --prefix=" + tools.unix_path(self.package_folder))
                tools.run_in_windows_bash(self, subsystem="msys", cwd=".", msys_mingw=True, bashcmd="make")
                tools.run_in_windows_bash(self, subsystem="msys", cwd=".", msys_mingw=True, bashcmd="make install")
                os.remove(tools.get_env("CONAN_SH_PATH")) 
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.run("copy  Makefile.vc pkgsrc ")
                self.run("cd pkgsrc && nmake -f Makefile.vc all")
                self.run("cd pkgsrc && nmake -f Makefile.vc  prefix=\"" + self.package_folder + "\" install")

            if tools.get_env("BUILD_ENV") == "msvc32":
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    self.run("CC=cl ; ./configure --disable-asm  --enable-shared   --prefix=" +  tools.unix_path(self.package_folder),win_bash=True)
                    self.run("make",win_bash=True)
                    self.run("make install",win_bash=True)

            if tools.get_env("BUILD_ENV") == "msv64":
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    self.run(" ./configure --help",win_bash=True)
                    self.run("CC=cl ; ./configure --disable-asm --enable-static--prefix=" +  tools.unix_path(self.package_folder),win_bash=True)
                    self.run("make",win_bash=True)
                    self.run("make install",win_bash=True)

    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)


    def package_info(self):
        self.cpp_info.name = 'libx264'
        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "libx264"
                self.cpp_info.names["cmake_find_package_multi"] = "libx264"
                self.cpp_info.libdirs = ['pkgsrc']  # Directories where libraries can be found
                self.cpp_info.libs = ['libx264.a']  # The libs to link against
                self.cpp_info.buildirs = 'pkgsrc'
            if (tools.get_env("BUILD_ENV") == "msvc64") or (tools.get_env("BUILD_ENV") == "msvc32"):
                print("msvc package")
                self.cpp_info.names["cmake_find_package"] = "libx264"
                self.cpp_info.names["cmake_find_package_multi"] = "libx264"
                self.cpp_info.libs = ['libx264.lib']  # The libs to link against
                